package org.gangcai.mac.shop.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SPUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.classic.common.MultipleStatusView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.activity.BillDetailActivity;
import org.gangcai.mac.shop.activity.ShopReSaleActivity;
import org.gangcai.mac.shop.activity.TTShopDetailActivity;
import org.gangcai.mac.shop.activity.WebActivity;
import org.gangcai.mac.shop.adapter.AnimationAdapter2;
import org.gangcai.mac.shop.adapter.AnimationAdapter3;
import org.gangcai.mac.shop.api.AppAPI;
import org.gangcai.mac.shop.bean.HomeDataBean;
import org.gangcai.mac.shop.bean.OrderBean;
import org.gangcai.mac.shop.bean.PostsArticleBaseBean;
import org.gangcai.mac.shop.constants.Constant;
import org.gangcai.mac.shop.manager.RetrofitManager;
import org.gangcai.mac.shop.manager.RetrofitManageres;
import org.gangcai.mac.shop.oberver.CommonObserver;
import org.gangcai.mac.shop.rxlife.RxFragment;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;

public class OrderListsFragment extends RxFragment implements OnLoadmoreListener, OnRefreshListener {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.multiple_status_view)
    MultipleStatusView multipleStatusView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    Unbinder unbinder;
    private AnimationAdapter2 animationAdapter2;
    private AnimationAdapter3 animationAdapter3;
    private String mParam1;
    private String mParam2;
    private ArrayList<OrderBean.InfoBean> dataList;
    private CommonAdapter<OrderBean.InfoBean> adapter;
    /**
     * 默认是已付款
     */
    private String type = "1";

    public OrderListsFragment() {
        // Required empty public constructor
    }

    public static OrderListsFragment newInstance(int param1, String param2) {
        OrderListsFragment fragment = new OrderListsFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = String.valueOf(2+getArguments().getInt(ARG_PARAM1));
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_order_list, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        refreshLayout.setOnRefreshListener(this);

        refreshLayout.setOnLoadmoreListener(this);
        refreshLayout.setEnableLoadmore(false);
        refreshLayout.autoRefresh();
    }

    public void refreshData(int position){
        type=String.valueOf( 2+position );
        loadData();
    }

    private void loadData() {
//        multipleStatusView.showContent();
        String cidString=type;
        if(type.equals( "5" )){
            cidString="16";
        }
        RetrofitManageres.getInstance().create(AppAPI.class).HomeData("Portal","list","indexJson",cidString)
                .observeOn(AndroidSchedulers.mainThread())
                .compose(this.<HomeDataBean>bindToLifecycle())
                .subscribe(new CommonObserver<HomeDataBean>(refreshLayout) {
                    @Override
                    public void onSuccess(HomeDataBean value) {
                        if (value.getCode() == Constant.CODE_SUCC) {
                            if(type.equals( "5" )){
                                animationAdapter3 = new AnimationAdapter3(getActivity(),value.getReferer().getPosts());
                                animationAdapter3.openLoadAnimation();
                                animationAdapter3.setNotDoAnimationCount(3);
                                recyclerview.setLayoutManager(new GridLayoutManager(getActivity(), 2));
                                animationAdapter3.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                                    @Override
                                    public void onLoadMoreRequested() {

                                    }
                                });
                                animationAdapter3.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                        PostsArticleBaseBean postsArticleBaseBean=value.getReferer().getPosts().get( position );
                                        Bundle bundle = new Bundle();
                                        String postID=postsArticleBaseBean.getObject_id();
                                        String cid=postsArticleBaseBean.getTerm_id();
                                        String webString="http://thinkcmf.500-china.com/index.php?g=&m=article&a=index&id="+postID+"&cid="+cid;
                                        bundle.putString("webString", webString);
                                        bundle.putString("title" , postsArticleBaseBean.getPost_title());
                                        bundle.putString("postID" , postID);
                                        ActivityUtils.startActivity(bundle, getActivity(), WebActivity.class);
                                    }
                                });
                                recyclerview.setAdapter(animationAdapter3);
                            }else{
                            animationAdapter2 = new AnimationAdapter2(getActivity(),value.getReferer().getPosts(),type);
                            animationAdapter2.openLoadAnimation();
                            animationAdapter2.setNotDoAnimationCount(3);
                            recyclerview.setLayoutManager(new GridLayoutManager(getActivity(), 1));
                            animationAdapter2.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                                @Override
                                public void onLoadMoreRequested() {

                                }
                            });
                            animationAdapter2.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                    PostsArticleBaseBean postsArticleBaseBean=value.getReferer().getPosts().get( position );
                                    Bundle bundle = new Bundle();
                                    bundle.putString( "tid",postsArticleBaseBean.getTid() );
                                    bundle.putString( "term_id",postsArticleBaseBean.getTerm_id() );
                                    bundle.putString( "post_id",postsArticleBaseBean.getObject_id() );
                                    bundle.putString( "post_title",postsArticleBaseBean.getPost_title() );
                                    bundle.putString( "post_authorname",postsArticleBaseBean.getPost_authorname() );
                                    bundle.putString( "mobile",postsArticleBaseBean.getMobile() );
                                    bundle.putString( "post_mobile",postsArticleBaseBean.getPost_mobile() );
                                    bundle.putString( "post_area",postsArticleBaseBean.getPost_area() );
                                    bundle.putString( "post_price",postsArticleBaseBean.getPost_price() );
                                    bundle.putString( "post_date",postsArticleBaseBean.getPost_date() );
                                    bundle.putString( "photos_urls",postsArticleBaseBean.getPhotos_urls() );
                                    bundle.putString( "photos_content",postsArticleBaseBean.getPost_excerpt() );
                                    ActivityUtils.startActivity(bundle, getActivity(), TTShopDetailActivity.class);
                                }
                            });
                            recyclerview.setAdapter(animationAdapter2);
                        }
                        }
                    }
                });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        loadData();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        loadData();
    }
}
